<?
/**
 * VoiceOne Settings/Tech/
 *
 * Frontend to provide access to the tech system in voiceone.
 * @package voiceone
 * @subpackage  settings_tech
 */
  session_start();
  include ("../../../admin/config/cfg.php");
  global $cwlang, $relBasePath, $cwLanguage;
  
  //================ POST & GET ================
  $alert=getAlert();
  // move top channel
  if ($_GET["cmd"]=="top") {
    $ret=$asterisk->moveTrunk($_GET["id"],1,1);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Trunk Moved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Trunk not moved")."</div>";
    goToMainPage($alert,"./?id=".$_GET["id"]);
  }
  // move up channel
  if ($_GET["cmd"]=="up") {
    $ret=$asterisk->moveTrunk($_GET["id"],1,0);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Trunk Moved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Trunk not moved")."</div>";
    goToMainPage($alert,"./?id=".$_GET["id"]);
  }
  // move down channel
  if ($_GET["cmd"]=="down") {
    $ret=$asterisk->moveTrunk($_GET["id"],2,0);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Trunk moved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Trunk not moved")."</div>";
    goToMainPage($alert,"./?id=".$_GET["id"]);
  }
  // move bottom channel
  if ($_GET["cmd"]=="bottom") {
    $ret=$asterisk->moveTrunk($_GET["id"],2,1);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Trunk moved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Trunk not moved")."</div>";
    goToMainPage($alert,"./?id=".$_GET["id"]);
  }
  // set flash
  if((isset($_GET["id"]))&&($_GET["id"]!=0)){
    $scriptFade="<script>Fat.fade_element(\"row".$_GET["id"]."\", 30, 500, \"#FFFF33\", \"#FFFFFF\");</script>";
  }
  
  //================ PAGE LOGIC ================
  // get the technologies list
  $tech=$asterisk->getTechnology(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  // get the trunks list
  $ret=$asterisk->getTrunk(0,0);
  if ($err=$asterisk->getError()) die("Error: $err");
  // create html
  $count=1;
  if ((is_array($ret))&&(is_array($tech))) {
    foreach($ret as $v) {
      $id=$v["id"];
      $name=$v["name"];
      $speeddial=$v["speeddial"];
      $technology=$v["technology"];
      $allow_incoming=$v["allow_incoming"];
      $allow_outgoing=$v["allow_outgoing"];
      $key=$v["key"];
      foreach ($tech as $t) if ($t["id"]==$technology) $techName=$t["name"];
      $key=$v["key"];
      //create link
      $entityArr=$asterisk->getEntity($key);
      $entityType=$entityArr[0]["type"];
      if ($entityType==MISDN_GROUP) $link="../misdn/edit/?id=$key";
      else if ($entityType==ZAPATA_GROUP) $link="../zapata/edit/?id=$key";
      else if ($entityType==PROVIDER_ACCOUNT) $link="../providers/account/?id=$key";
      else if ($entityType==OFFICE) $link="../offices/edit/?id=$key";
      else $link="#";
      //buttons Up and Down      
      if ($count==1) $button="<td>&nbsp;</td><td>&nbsp;</td>";
      else $button="<td><a href=\"?cmd=top&id=$id\"><img src=\"$relBasePath/public/img/icon_top.gif\" alt=\"top\" /></a></td><td><a href=\"?cmd=up&id=$id\"><img src=\"$relBasePath/public/img/icon_up.gif\" alt=\"up\" /></a></td>";
      if ($count==count($ret)) $button.="<td>&nbsp;</td><td>&nbsp;</td>";
      else $button.="<td><a href=\"?cmd=down&id=$id\"><img src=\"$relBasePath/public/img/icon_down.gif\" alt=\"down\" /></a></td><td><a href=\"?cmd=bottom&id=$id\"><img src=\"$relBasePath/public/img/icon_bottom.gif\" alt=\"bottom\" /></a></td>";
      //allow incoming/outgoing
      if($allow_incoming==1) $check_incoming="<input type=\"checkbox\" name=\"[$id]allow_incoming\" id=\"[$id]allow_incoming\" value=\"1\" checked=\"checked\">";
      else $check_incoming="<input type=\"checkbox\" name=\"[$id]allow_incoming\" id=\"[$id]allow_incoming\" value=\"1\">";
      if($allow_outgoing==1) $check_outgoing="<input type=\"checkbox\" name=\"[$id]allow_outgoing\" id=\"[$id]allow_outgoing\" value=\"1\" checked=\"checked\">";
      else $check_outgoing="<input type=\"checkbox\" name=\"[$id]allow_outgoing\" id=\"[$id]allow_outgoing\" value=\"1\">";
      //table line
      $html.="<tr id=\"row$id\" class=\"firstofgroup rule\">";
      $html.="<td class=\"green\">$id</td>";
      $html.="$button";
      $html.="<td><strong>$speeddial</strong></td>";
      $html.="<td>$techName</td>";
      $html.="<td ><strong><a href=\"$link\">$name</a></strong></td>";
      /*
      $html.="<td>$check_incoming</td>";
      $html.="<td>$check_outgoing</td>";
      */
      $html.="</tr>\n";
      $count++;
    }
  }    
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>

  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Available Lines")?></h1>
  </div>
  <p class="blockintro"><?=T_("Ready to use line. The entries below list the available lines you can use inside VoiceOne.")?></p>
  <div class="block">
  <table class="todolist" cellspacing="0" >
    <tr>
      <th><?=T_("Id")?></th>
      <th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th>
      <th><?=T_("Speed Dial")?></th>
      <th><?=T_("Technology")?></th>
      <th><?=T_("Line name")?></th>
      <!--
      <th><?=T_("Allow Incoming")?></th>
      <th><?=T_("Allow Outgoing")?></th>
      -->
    </tr>
    <?=$html?>
  </table>
  </div>
  
  <?
  echo $scriptFade;
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="alltrunks";include("../sidebar.php");?>
      <h1><?=T_("Custom Line")?></h1>
      <p><?=T_("Sign here your custom line, so you can use outside resources.")?></p>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "alltrunks");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

